#pragma once
#include "const.h"
#include <jdbc/mysql_connection.h>
#include <jdbc/mysql_driver.h>
#include <jdbc/cppconn/prepared_statement.h>
#include <jdbc/cppconn/resultset.h>
#include <jdbc/cppconn/statement.h>
#include <jdbc/cppconn/exception.h>
#include "configMgr.h"

class SqlConnection 
{
public:
	SqlConnection(sql::Connection* con, int64_t lasttime);
	std::unique_ptr<sql::Connection> _con;
	int64_t _last_oper_time;
};
class mysqlPool
{
public:
	mysqlPool(const std::string& url, const std::string& user, const std::string& pass, const std::string& schema, int poolsize);
	void checkConnection();
	std::unique_ptr<SqlConnection> getConnection();
	void returnConnection(std::unique_ptr<SqlConnection>);
	void Close();
	~mysqlPool();
private:
	std::string _url;
	std::string _user;
	std::string _pass;
	std::string _schema;
	int _poolsize;
	std::queue<std::unique_ptr<SqlConnection>> _pool;
	std::mutex _mtx;
	std::condition_variable _condvar;
	std::atomic<bool> _b_stop;
	std::thread _check_thread;
};

class mysqlDao {
public:
	mysqlDao();
	~mysqlDao();
	int RegUser(const std::string& name, const std::string& email, const std::string& pwd);
	bool CheckEmail(const std::string& user, const std::string& email);
	bool UpdatePwd(const std::string& name, const std::string& pwd);
	bool CheckPwd(const std::string& user, const std::string& pwd,UserInfo& userinfo);
private:
	std::unique_ptr<mysqlPool> _pool;
};

